package com.cpi.cornpp.creditline.services;

import com.cpi.cornpp.creditline.converter.CreditLineConverter;
import com.cpi.cornpp.creditline.dao.CreditLineDAO;
import com.cpi.cornpp.creditline.dto.CreditLineDTO;
import com.cpi.cornpp.creditline.exception.CreditLineException;
import com.cpi.cornpp.creditline.view.CreditLineView;
import com.cpi.cornpp.plants.exception.PlantException;

/**
 * Credit Line Service
 * @author Edgar Joao
 *
 */
public class CreditLineService {

	/**
	 * Exist Credit Line
	 * @author Edgar Joao
	 * @return
	 * @throws CreditLineException
	 */
	public boolean existCreditLine() throws CreditLineException {
		CreditLineDAO creditLineDAO = null;				
		boolean exist = false;
		
		creditLineDAO = new CreditLineDAO();		
		try {
			if(creditLineDAO.existCreditLine()){
				exist = true;
			}else {
				exist = false;
			}
		} catch (CreditLineException pe) {
			throw pe;
		} catch (Exception e) {
			CreditLineException pe = new CreditLineException(e,
					PlantException.LAYER_SERVICE,
					PlantException.ACTION_SELECT);
			e.printStackTrace(System.out);
			throw pe;
		}			
		return exist;				
	}
	
	
	/**
	 * Insert Credit Line
	 * @author Edgar Joao
	 * @param view
	 * @throws CreditLineException
	 */
	public void addCreditLine(CreditLineView creditLineView) throws CreditLineException{
		CreditLineDAO creditLineDAO = null;
		CreditLineConverter creditLineConverter = null;
		CreditLineDTO creditLineDTO = null;
		
		if(creditLineView != null){
			creditLineDAO = new CreditLineDAO();
			creditLineConverter = new CreditLineConverter();
			creditLineDTO = creditLineConverter.converterCreditLineViewToDTO(creditLineView);
			
			try {
				creditLineDAO.addCreditLine(creditLineDTO);
				
			} catch (CreditLineException pe) {
				throw pe;
			} catch (Exception e) {
				CreditLineException pe = new CreditLineException(e,
						PlantException.LAYER_SERVICE,
						PlantException.ACTION_INSERT);
				e.printStackTrace(System.out);
				throw pe;
			}				
		}
	}
	
	
	/**
	 * Update Credit Line
	 * 
	 * @author Edgar Joao
	 * @param creditLineView
	 * @throws CreditLineException
	 */
	public void updateCreditLine(CreditLineView creditLineView) throws CreditLineException{
		CreditLineDAO creditLineDAO = null;
		CreditLineConverter creditLineConverter = null;
		CreditLineDTO creditLineDTO = null;
		
		if(creditLineView != null){
			creditLineDAO = new CreditLineDAO();
			creditLineConverter = new CreditLineConverter();
			creditLineDTO = creditLineConverter.converterCreditLineViewToDTO(creditLineView);
			
			try {
				creditLineDAO.updateCreditLine(creditLineDTO);				
			} catch (CreditLineException pe) {
				throw pe;
			} catch (Exception e) {
				CreditLineException pe = new CreditLineException(e,
						PlantException.LAYER_SERVICE,
						PlantException.ACTION_UPDATE);
				e.printStackTrace(System.out);
				throw pe;
			}				
		}		
	}
	
	
	/**
	 * Get Credit Line
	 * @author Edgar Joao
	 * @return
	 * @throws CreditLineException
	 */
	public CreditLineView getCredtiLineDTO() throws CreditLineException{
		CreditLineDAO creditLineDAO = null;						
		CreditLineConverter creditLineConverter = null;
		CreditLineView creditLineView = null;
		
		creditLineDAO = new CreditLineDAO();	
		creditLineConverter = new CreditLineConverter();
		
		try {
		 creditLineView = new CreditLineView();
		 creditLineView = creditLineConverter.converterCreditLineDtoToView(creditLineDAO.getCredtiLineDTO());
			
		} catch (CreditLineException pe) {
			throw pe;
		} catch (Exception e) {
			CreditLineException pe = new CreditLineException(e,
					PlantException.LAYER_SERVICE,
					PlantException.ACTION_SELECT);
			e.printStackTrace(System.out);
			throw pe;
		}			
		return creditLineView;				
	}
}
